import * as mobx from 'mobx';
import { ListStore } from './listStore';
import { WeaTools , WeaLocaleProvider} from 'ecCom';
import { WeaForm ,WeaTableNew} from 'comsMobx';
const { TableStore } = WeaTableNew;
import * as API_MONITOR from '../apis/monitor';
import isEmpty from 'lodash/isEmpty';

const getLabel = WeaLocaleProvider.getLabel;
const observable = mobx.observable;
const action = mobx.action;
const Base64 = WeaTools.Base64;
/**
 * 
 * 
 */
 
export class WFMonitorStore extends ListStore{

    urlParams = {};
    searchParams = {
        createdateselect:0
    };
    @observable forceRender = 1;
    @observable switchForm = {};
    selectedRowKeys = [];
    resultFormValue = {};
    @observable leftLayoutShow = false;

    @observable btnObj = {key:'batchDelete'};


    constructor(){
        super();
        this.topTab=[{color: "#000000", groupid: "flowAll", showcount: false, title: getLabel(82857,'全部'), createdateselect: 0},
                    {color: "#000000", groupid: "flowTaday", showcount: false, title: getLabel(15537,'今天'), createdateselect: 1},
                    {color: "#000000", groupid: "flowWeek", showcount: false, title: getLabel(15539,'本周'), createdateselect: 2},
                    {color: "#000000", groupid: "flowMonth", showcount: false, title: getLabel(15541,'本月'), createdateselect: 3},
                    {color: "#000000", groupid: "flowSeason", showcount: false, title: getLabel(21904,'本季'), createdateselect: 4},
                    {color: "#000000", groupid: "flowYear", showcount: false, title: getLabel(15384,'本年'), createdateselect: 5}]
        this.setTitle(getLabel(385194,'流程监控'));
        this.leftTree = WeaTools.ls.getJSONObj("listMonitorLeftTree") || [];
        this.setUrlParams = this.setUrlParams.bind(this);
        this.appendSearchParams = this.appendSearchParams.bind(this);
        this.loadCondition = this.loadCondition.bind(this);
        this.resetConditionValue = this.resetConditionValue.bind(this);
        this.doSearch = this.doSearch.bind(this);
        this.loadTree = this.loadTree.bind(this);
        this.setConditionByTree = this.setConditionByTree.bind(this);
        this.resetCondition = this.resetCondition.bind(this);
        this.initDatas = this.initDatas.bind(this);
        this.recoverQueryStore = this.recoverQueryStore.bind(this);
        this.reLoad = this.reLoad.bind(this);
        this.clickButttonDrop = this.clickButttonDrop.bind(this);
    }

    @action
    setUrlParams(queryParams = {}){
        this.urlParams = queryParams;
        if(this.urlParams.tabTitle){
            this.title = Base64.decode(decodeURIComponent(this.urlParams.tabTitle));
        }
    }

    initDatas(){
        this.searchParams.createdateselect = 0;
        this.setSelectedTreeKey("");
        this.setShowSearchAd(false);
    }
    appendSearchParams(params = {}){
        this.searchParams = {...this.searchParams, ...params}
    }

    reLoad(params = {}){
        const { needReloadList = true } = params;
        if(needReloadList){
            const current =  this.tableStore.current;
            const sortParams = this.tableStore.sortParams.toJS();
            this.doSearch({current, sortParams});
        }
    }
    
    loadCondition(params={}){
        this.loading = true;
        this.setTitle(getLabel(385194,'流程监控'));
        if(this.urlParams.tabTitle){
            this.title = Base64.decode(decodeURIComponent(this.urlParams.tabTitle));
        }
        this.setShowSearchAd(false);
        API_MONITOR.getMonitorConditionInfo(this.urlParams).then((data)=>{
            this.loading = false;
            if("pagetitle" in data)
                this.title = data.pagetitle;
            let condition = data.conditioninfo;
            WeaTools.ls.set("listMonitorCondition", condition);
            this.condition = condition;
            this.form = new WeaForm();
            this.form.initFormFields(condition);
        });
    }

    resetConditionValue(){
        this.clearFormFields();     //清除固定条件的form值
    } 
    recoverQueryStore(store){
        if(isEmpty(store.condition)){
            this.loadCondition();
        }else{
            this.title = store.title;
            this.condition = store.condition;
            this.form = new WeaForm();
            this.form.initFormFields(store.condition);
            this.form.updateFields(store.form.getFormDatas() || {} , true);
        }
        
    }
    doSearch(params = {}){
        this.tableStore = new TableStore();
        this.tableStore.tableUpdate({ checkId: this.btnObj.key || '' });
        this.rewriteTableStore();
        let queryParams = {};
        
        const searchParamsCommon = this.form.getFormParams();
        const changeTitleParams = this.form.getFormDatas();
        this.resultFormValue =  changeTitleParams;
        if(changeTitleParams.workflowid){//105
            if(changeTitleParams.workflowid.valueSpan){
                this.setTitle(`${changeTitleParams.workflowid.valueSpan}`)
            }else{
                if(changeTitleParams.typeid){
                    if(changeTitleParams.typeid.valueSpan){
                        this.setTitle(`${changeTitleParams.typeid.valueSpan}`);
                    }else{
                        if(!this.urlParams.tabTitle)
                            this.setTitle(getLabel(385194,'流程监控'));
                    }
                }
            }
        }else{//其他
            if(changeTitleParams.typeid){
                if(changeTitleParams.typeid.valueSpan){
                    this.setTitle(`${changeTitleParams.typeid.valueSpan}`);
                }else{
                    if(!this.urlParams.tabTitle)
                        this.setTitle(getLabel(385194,'流程监控'));
                }
            }else{
                if(!this.urlParams.tabTitle)
                    this.setTitle(getLabel(385194,'流程监控'));
            }
        }

        let searchParamsAd = {};

        queryParams = {
          ...this.urlParams, 
          ...this.searchParams, 
          ...searchParamsCommon, 
          ...searchParamsAd, 
          ...queryParams,
          ...params
        };
        this.loading = true;
        API_MONITOR.getMonitorSessionkey(queryParams).then((data)=>{
            this.loading = false;
            this.tableStore.getDatas(data.sessionkey, params.current || 1, "", params.sortParams || []);
            this.searchParams = { ...this.searchParams, ...params };
            this.searchParams.createdateselect=searchParamsCommon.createdateselect || params.createdateselect || 0;
        });
    }

    loadTree(params = {}){
        let queryParams = {};
        //常用查询条件值
        const searchParamsCommon = this.form.getFormParams();
        //自定义查询条件值
        let searchParamsAd = {};
        
        //拼接查询请求参数
        // queryParams = {...this.urlParams,...this.searchParams, ...searchParamsCommon, ...searchParamsAd, ...queryParams,...params};
        queryParams = {...this.urlParams, ...params};
        API_MONITOR.getTree(queryParams).then((data)=>{
            WeaTools.ls.set("listMonitorLeftTree", data.treedata);
            this.leftTree = data.treedata;
            this.leftTreeCount = data.treecount;
            this.leftTreeCountType = data.countcfg;
            this.forceRender++;
        });
    }


    clickButttonDrop(key){
        this.tableStore.tableUpdate({checkId:key, selectedRowKeys:[]});
        this.tableStore.reloadCurrent(()=>{
            this.btnObj = {key:key, disabled:true};
        });
    }


    setConditionByTree(){
        const key = this.selectedTreeKey;
        if(!key)
            return;
        //选中树节点同步修改高级查询条件
        // const searchParamsCommon = this.form.getFormParams();
        const searchParamsCommon = {};
        let formFields = {...searchParamsCommon};
        if(key.indexOf("wf_") === 0){
            const workflowid = key.substring(3);
            let workflowname = '';
            this.leftTree && this.leftTree.map(t =>{
                t.childs && t.childs.map(w =>{
                    if(w.domid === key)
                        workflowname = w.name;
                });
            });
            formFields["workflowid"] = {value:workflowid, valueSpan:workflowname};
        }else if(key.indexOf("type_") === 0){
            const workflowtype = key.substring(5);
            let typename = '';
            this.leftTree && this.leftTree.map(t =>{
                if(t.domid === key)
                    typename = t.name;
            });
            formFields["typeid"] = {value:workflowtype, valueSpan:typename};
        }
        this.setFormFields(formFields);
    }

    resetCondition(){
        this.setTitle(getLabel(385194,'流程监控'));
        if(this.urlParams.tabTitle){
            this.title = Base64.decode(decodeURIComponent(this.urlParams.tabTitle));
        }
        this.clearFormFields();
        this.btnObj = {key:'batchDelete'};
    }
    @action('控制左右布局左侧显隐')
    setLeftLayoutShow = (bool) => {
        this.leftLayoutShow = bool;
    }
}